package com.tbit.uqbike.client.service.impl;

import com.tbit.uqbike.client.constant.MapConstant;
import com.tbit.uqbike.client.constant.ReturnPosLogConstant;
import com.tbit.uqbike.client.constant.UserFromConstant;
import com.tbit.uqbike.client.dao.core.MachineDao;
import com.tbit.uqbike.client.dao.core.MachineLastUseDao;
import com.tbit.uqbike.client.dao.log.BorrowPosLogDao;
import com.tbit.uqbike.client.dao.log.MachineLastUseLogDao;
import com.tbit.uqbike.client.pojo.Machine;
import com.tbit.uqbike.client.pojo.MachineLastUse;
import com.tbit.uqbike.client.pojo.MachineLastUseLog;
import com.tbit.uqbike.client.pojo.Point;
import com.tbit.uqbike.client.pojo.borrowbike.BorrowPosLog;
import com.tbit.uqbike.client.service.CalibrateService;
import com.tbit.uqbike.client.service.MachineLastUseService;
import com.tbit.uqbike.client.util.DateTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 设备最后使用
 *
 * @author Leon
 * 2019年11月17日 下午2:40:35
 */
@Service("machineLastUseService")
public class MachineLastUseServiceImpl implements MachineLastUseService {
    @Autowired
    private MachineLastUseDao machineLastUseDao;
    @Autowired
    private CalibrateService calibrateService;
    @Autowired
    private MachineLastUseLogDao machineLastUseLogDao;
    @Autowired
    private MachineDao machineDao;
    @Autowired
    private BorrowPosLogDao borrowPosLogDao;

    @Override
    public MachineLastUse getByMachineId(Integer machineId) {
        return machineLastUseDao.getByMachineId(machineId);
    }

    @Override
    public void insert(MachineLastUse machineLastUse) {
        machineLastUseDao.insert(machineLastUse);
    }

    @Override
    public void update(MachineLastUse machineLastUse) {
        machineLastUseDao.update(machineLastUse);
    }

    @Override
    public Long updateLastInfo(Integer machineId, String machineNO, Point point, Integer userId, String phone, Integer mapType) {
        /**校准经纬度*/
        Point pointC = calibrateService.calibrate(point, mapType, MapConstant.MAP_ORI);

        MachineLastUse machineLastUse = new MachineLastUse();
        machineLastUse.setMachineId(machineId);
        machineLastUse.setMachineNO(machineNO);
        machineLastUse.setUserId(userId);
        machineLastUse.setPhone(phone);
        machineLastUse.setLo(pointC.getX());
        machineLastUse.setLa(pointC.getY());
        machineLastUse.setUpdateTime(DateTimeUtil.getNowTime());

        MachineLastUse machineLastUseDB = getByMachineId(machineId);
        if (machineLastUseDB == null) {
            insert(machineLastUse);
        } else {
            update(machineLastUse);
        }

        Machine machine = machineDao.getByMachineId(machineId);
        /**记录最后10个扫码人信息*/
        MachineLastUseLog machineLastUseLog = new MachineLastUseLog();
        machineLastUseLog.setAddTime(new Date());
        machineLastUseLog.setLa(pointC.getY());
        machineLastUseLog.setLo(pointC.getX());
        machineLastUseLog.setMachineId(machineId);
        machineLastUseLog.setMachineNO(machineNO);
        machineLastUseLog.setPhone(phone);
        machineLastUseLog.setUserId(userId);
        machineLastUseLog.setUseType(UserFromConstant.USERFROM_CLIENT);
        machineLastUseLog.setUserCode(machine.getUserCode());
        machineLastUseLogDao.insert(machineLastUseLog);

        BorrowPosLog borrowPosLog = new BorrowPosLog();
        borrowPosLog.setLat(pointC.getY());
        borrowPosLog.setLon(pointC.getX());
        borrowPosLog.setMachineId(machineId);
        borrowPosLog.setMachineNo(machineNO);
        borrowPosLog.setPosFrom(ReturnPosLogConstant.TYPE_PGONE);
        borrowPosLog.setPosTime(new Date());
        borrowPosLog.setUserCode(machine.getUserCode());
        borrowPosLogDao.insert(borrowPosLog);

        return borrowPosLog.getId();

    }

    @Override
    public void updateLastUse(Integer machineId, String machineNO, Point point, Integer userId,
                              String phone) {
        MachineLastUse machineLastUse = new MachineLastUse();
        machineLastUse.setMachineId(machineId);
        machineLastUse.setMachineNO(machineNO);
        machineLastUse.setUserId(userId);
        machineLastUse.setPhone(phone);
        machineLastUse.setLo(point.getX());
        machineLastUse.setLa(point.getY());
        machineLastUse.setUpdateTime(DateTimeUtil.getNowTime());

        MachineLastUse machineLastUseDB = getByMachineId(machineId);
        if (machineLastUseDB == null) {
            insert(machineLastUse);
        } else {
            update(machineLastUse);
        }
    }

}
